//
//  RootViewController.m
//
//  Created by Sean Wang on 12-4-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "FirstViewController.h"
#import "FeaturesTableViewController.h"
#import "WhereToBuyViewController.h"
#import "AboutTableViewController.h"

static WhereToBuyViewController *whereToBuyViewController;

@implementation RootViewController

@synthesize theTableView = _theTableView;

+(WhereToBuyViewController*)getWhereToBuyViewController
{
    return whereToBuyViewController;
}

- (UIBarButtonItem *) createLeftBarButton{
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 36, 23)];
    [leftButton setImage:[UIImage imageNamed:@"btn-house"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"btn-houseb"] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(moveToHomePage) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    return leftItem;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {                
        UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
        UINavigationController *naviController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
        viewController1.navigationItem.leftBarButtonItem = [self createLeftBarButton];
        naviController1.navigationBar.barStyle = UIBarStyleBlack;
        
        UIViewController *viewController2 = [[FeaturesTableViewController alloc] initWithStyle:UITableViewStylePlain];
        UINavigationController *naviController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
        viewController2.navigationItem.leftBarButtonItem = [self createLeftBarButton];
        naviController2.navigationBar.barStyle = UIBarStyleBlack;
        
        whereToBuyViewController = [[WhereToBuyViewController alloc] initWithNibName:@"WhereToBuyViewController" bundle:nil];
        UINavigationController *naviController3 = [[UINavigationController alloc] initWithRootViewController:whereToBuyViewController];
        whereToBuyViewController.navigationItem.leftBarButtonItem = [self createLeftBarButton];
        naviController3.navigationBar.barStyle = UIBarStyleBlack;
        
        UIViewController *viewController4 = [[AboutTableViewController alloc] initWithStyle:UITableViewStylePlain];
        UINavigationController *naviController4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
        viewController4.navigationItem.leftBarButtonItem = [self createLeftBarButton];
        naviController4.navigationBar.barStyle = UIBarStyleBlack;
        
        tabBarController = [[UITabBarController alloc] init];
        tabBarController.viewControllers = [NSArray arrayWithObjects:naviController1, naviController2, naviController3, naviController4, nil];
        tabBarController.view.frame = self.view.frame;
        tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        tabBarController.view.backgroundColor = [UIColor lightGrayColor];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];  
    CGRect tableFrame = CGRectMake(25, 205, 275, 176);
    UITableView *tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    
    // set the autoresizing mask so that the table will always fill the view
	tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
	
	// set the cell separator to a single straight line.
	tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    tableView.separatorColor = [UIColor clearColor];
    //tableView.backgroundColor = [UIColor clearColor];
    tableView.scrollEnabled = NO;
	
	// set the tableview delegate to this object and the datasource to the datasource which has already been set
	tableView.delegate = self;
	tableView.dataSource = self;
    
	// set the tableview as the controller view
    self.theTableView = tableView;
    [self.view addSubview:self.theTableView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table View datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *label = nil;
    if (cell == nil) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
        CGRect frame = CGRectMake(50, 10, 180, 25);
        label = [[UILabel alloc] initWithFrame:frame];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont boldSystemFontOfSize:12];
        
        [cell addSubview:label];
	}
                
    NSString *highlightImageName = nil;
    NSString *imageName = nil;
    switch (indexPath.row) {
        case 0:{
            imageName = @"home-row1.png";
            highlightImageName = @"home-row1b.png";
            label.text = @"Sizing Wizard";
            break;
        }
        case 1:{
            label.text = @"Air Conditioning Features";
            imageName = @"home-row2.png";
            highlightImageName = @"home-row2b.png";
            break;
        }
        case 2:
            label.text = @"Where to Buy";
            imageName = @"home-row3.png";
            highlightImageName = @"home-row3b.png";
            break;
        case 3:
            label.text = @"About";
            imageName = @"home-row4.png";
            highlightImageName = @"home-row4b.png";
            break;
        default:
            break;
    }
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    UIImageView *highlightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:highlightImageName]];
    cell.backgroundView = imageView;
    cell.selectedBackgroundView = highlightImageView; 
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor blackColor];    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath)
    {
        if (tabBarController && indexPath.row < [[tabBarController viewControllers] count])
        {
            [tabBarController setSelectedIndex:indexPath.row];
            [self moveToTabBar];
        }
    }
}

- (void)moveToHomePage
{
    [self dismissViewControllerAnimated:YES completion:Nil];
}

- (void)moveToTabBar
{
    //NSLog(@"moveToTabBar");
    //static NSString *segueIdentifier = @"moveToTabBar";
    //UIStoryboardSegue *segue = [[UIStoryboardSegue alloc] initWithIdentifier:segueIdentifier source:self destination:tabBarController];
    [self presentViewController:tabBarController animated:YES completion:nil];
    
}

@end
